I am using datetime-local input type in HTML, but I don't want to use the "T" on the output. I want to only get the date and time with a space between.
<input type="datetime-local" id="date" name="date">
The output is:
2021-11-08T16:34
What i want is:
2021-11-08 16:34
What do i need to do to get this output?
PS: Try to help me without bootstrap libraries, because on this project i can't use it.
As Dima Parzhitsky wrote, consider to use Intl.DateTimeFormat methods
const
localDt =_=>
{
let now = new Date()
now.setMinutes(now.getMinutes() - now.getTimezoneOffset())
now.setSeconds(0) // remove seconds
now.setMilliseconds(0) // remove milliseconds
return now
}
, fxDate =
Intl.DateTimeFormat( undefined,
{ hour12: false, year: 'numeric', month: '2-digit'
, day: '2-digit', hour: '2-digit', minute: '2-digit'
})
, dtForm = d =>
{
let {day,month,year,hour,minute} =
fxDate.formatToParts(d).reduce((o,{type,value})=>(o[type]=value,o),{})
return `${year}-${month}-${day} ${hour}:${minute}`
}
dateInput.valueAsDate = localDt() // init date value
getDate.onclick =_=>
{
let dt = dateInput.valueAsDate
dt.setMinutes(dt.getMinutes() + dt.getTimezoneOffset())
dateStr.textContent = dtForm(dt)
}
<input type="datetime-local" id="dateInput">
<br><br>
<button id="getDate">get Date</button>
<p id="dateStr">???</p>
You can try this, I use pure javascript
//get the date
var x = document.getElementById("date").value;
//this replaces the 'T' with an space
x = x.replace("T", " ");
//this places the string back on the HTML page
document.getElementById("demo").innerHTML = x;